/** 删除数据 */
db.lg_resume_preview.remove({})
/** 插入数据 */
db.lg_resume_preview.insert({name:"张晓峰",birthday:new ISODate("2000-07-01"),gender:1,expectSalary:15000,city:"bj"})
db.lg_resume_preview.insert({name:"张震",birthday:new ISODate("1995-04-01"),gender:1,expectSalary:18000,city:"bj"})
db.lg_resume_preview.insert({name:"李山",birthday:new Date("1995-04-01"),gender:1,expectSalary:25000,city:"sh"})
db.lg_resume_preview.insert({name:"李笑来",birthday:new Date("1998-04-01 14:20:09"),gender:1,expectSalary:20000,city:"sh"})
db.lg_resume_preview.insert({name:null,birthday:new Date("1992-04-01 14:20:09"),gender:1,expectSalary:30000,city:"sh"}) 
db.lg_resume_preview.insert({birthday:new Date("1991-05-01 14:20:09"),gender:1,expectSalary:50000,city:"sz"})   

db.lg_resume_preview.insert(
    [{name:"李丽",birthday:new Date("1996-05-01 14:20:09"),gender:0,expectSalary:21000,city:"sz"},
     {name:"李平",birthday:new Date("1997-07-01 14:20:09"),gender:0,expectSalary:22000,city:"sz"}])
/** 数据查询 */
db.lg_resume_preview.find()
db.lg_resume_preview.find({expectSalary:18000})
db.lg_resume_preview.find({expectSalary:{$eq:18000}})
db.lg_resume_preview.find({expectSalary:{$ne:18000}})
db.lg_resume_preview.find()

db.lg_resume_preview.find({expectSalary:{$gt:18000}})
/*多条件 查询 */
db.lg_resume_preview.find({expectSalary:{$ne:18000},name:"李山"})

db.lg_resume_preview.find({expectSalary:{$gt:18000}})

/*分页查询 */
db.lg_resume_preview.find({}).sort({ expectSalary:-1 }).skip(2).limit(2)

/**数据更新  */
db.lg_resume_preview.update({name:"李珊珊"},{$inc: {expectSalary:20001} },{upsert:true} )
db.lg_resume_preview.find({name:"李珊珊"})
db.lg_resume_preview.update({name:"李珊珊"},{$inc: {expectSalary:20008} },{upsert:false} )

db.lg_resume_preview.update({name:"李珊珊"},{$set: {expectSalary:20008} },{upsert:false} )
  
/** 插入100 万条数据 做索引测试  */
for(var i=0;i<1000000;i++){
    db.lg_resume.insert({id:i,name:"test"+i,salary:(Math.random()*20000).toFixed(2)})
}
db.runCommand(
   {
      planCacheSetFilter: "lg_resume",
      query: { name: "test1" },
      indexes: [
        {name:1}
      ]
   }
)
db.textTextIndex.find({name:"test2"}).explain()

db.lg_resume.insert({id:-1,name:null,salary:(Math.random()*20000).toFixed(2)})
db.lg_resume.insert({id:-2,name:{},salary:(Math.random()*20000).toFixed(2)})
db.lg_resume.find({name:null})
db.lg_resume.find({name:{}})
db.lg_resume.find({name:{$in: [null],$exists: true}})

db.lg_resume.find({name:null}).explain() 

db.lg_resume.find({id:1}).explain().pretty();
  
db.lg_resume.find({name:{$in: [null,{}]}})

db.lg_resume.find({name:null}).explain("queryPlanner") 
db.lg_resume.find({name:"lisi"})
   .projection({})
   .sort({_id:-1})
   .limit(100)  
   
   
db.lg_resume_preview.find({name:{$in: [null],$exists: true}})   
  
/** */

db.lg_resume_preview.find({}).count()
db.lg_resume_preview.distinct("city").length

db.lg_resume_preview.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])

db.lg_resume_preview.aggregate([{$group : {_id: "$city", num_tutorial : {$sum : 1}}}])   
  
db.lg_resume_preview.aggregate(
    [{$group : {_id: "$city", "num_tutorial" : {$sum : 1}}},{$sort: {num_tutorial:-1}}])   

db.lg_resume_preview.aggregate(
    [{$group : {_id: "$city",  count : {$sum : 1}}},{$match: {count:{$gt:2}}]) 

db.lg_resume_preview.aggregate([{$group : {_id : "$city", city_count : {$sum : 1}}}])

db.lg_resume_preview.aggregate([{$group : {_id : "$city", city_count : {$sum : 1}}},
  {$project: {_id:0}}])
db.lg_resume_preview.aggregate([{$group : {_id : "$city", city_count : {$last : "$expectSalary"}}}]) 
  

  
db.lg_resume_preview.mapReduce(
   function() { emit(this.city,this.expectSalary); },
   function(key, values) {return Array.avg(values)},
   {  
      query:{expectSalary:{$gt: 22000}}
      out:"total"
   }
)
db.total.find()  
  